package asapIs134CK.womi.client.games.logoGame.froggitTheFrog.editor;

import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitSand;
import asapIs134CK.womi.client.imgs.logoGame.LogoGameImageBundle;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.VetoDragException;
import com.allen_sauer.gwt.dnd.client.drop.DropController;
import com.allen_sauer.gwt.dnd.client.drop.SimpleDropController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;

public class MapCell extends SimplePanel{
	private static final LogoGameImageBundle imgbundle = (LogoGameImageBundle) GWT.create(LogoGameImageBundle.class); 
	private final PickupDragController _dragCtrl;
	private final DropController _dropCtrl;//TODO !!!
	
	private class ChangeWidgetDropController extends SimpleDropController{
		
		private final MapCell _dropTarget;
		
		public ChangeWidgetDropController(MapCell dropTarget) {
			super(dropTarget);
			this._dropTarget = dropTarget;
		}
		
		@Override
		public void onDrop(DragContext context) {
			//System.out.println("MapCell:onDrop called");
			if (context.draggable instanceof CellGUI) {
				CellGUI draggedCell = (CellGUI) context.draggable;
				_dropTarget.setCell(draggedCell);
				//System.out.println("\tOK!");
				super.onDrop(context);
			}else{
				//System.out.println("\n\tthis is not a reconized widget to drop here :" +
				//		"\n\t\t->ignored !");
				//Window.alert("souscis avec le drop buddy");
			}
		}
		
		@Override
		public void onPreviewDrop(DragContext context) throws VetoDragException {
			super.onPreviewDrop(context);
			if (context.draggable instanceof CellGUI) {
				//that's ok !
			}else{
				//that's not ok !
				throw new VetoDragException();
			}
			
		}
		
	}
	
	
	
	//will get some function such as recievedrop and so on 

	public MapCell(PickupDragController dragCtrl){
		this.add(new CellGUI(new FroggitSand()));
		_dropCtrl = new ChangeWidgetDropController(this);
		_dragCtrl = dragCtrl;
		_dragCtrl.registerDropController(_dropCtrl);
		
	}
//	public MapCell(Widget wgt, PickupDragController dragCtrl){
//		_content = new SimplePanel();
//		_content.add(wgt);
//		initWidget(_content);
//		_dropCtrl = new ChangeWidgetDropController(this);
//		_dragCtrl = dragCtrl;
//		_dragCtrl.registerDropController(_dropCtrl);
//	}

	public void setCell(CellGUI c){
		this.clear();
		this.add(c);
	}
	public void restore(){
		this.clear();
		this.add(new Image(imgbundle.cell_sand_small()));
	}
}
